package io.gitee.yxsnake.embark.web.core.framework.controller;

import io.gitee.yxsnake.embark.web.core.base.Result;
import io.gitee.yxsnake.embark.web.core.base.ResultCode;
import io.gitee.yxsnake.embark.web.core.constant.Commons;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.hutool.core.text.StrUtil;
import org.slf4j.MDC;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * @author: snake
 * @create-time: 2024-09-29
 * @description: 通用 controller 用于业务 api 对外接口统一响应格式
 * @version: 1.0
 */
@Component
public class BaseController {

    @Resource
    protected  HttpServletRequest request;

    @Resource
    protected  HttpServletResponse response;

    /**
     * 页面跳转
     *
     * @param url 目标url
     * @return 跳转语句
     */
    protected String redirect(String url) {
        return StrUtil.format("redirect:{}", url);
    }

    /**
     * 成功返回
     *
     * @param object 返回对象
     * @param <T>    T
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> success(T object) {
        return success(object, null, HttpStatus.OK);
    }

    /**
     * 成功返回
     *
     * @return ResponseEntity
     */
    protected ResponseEntity success() {
        return success(HttpStatus.OK);
    }

    /**
     * 成功返回
     *
     * @param status HttpStatus对象
     * @param <T>    T
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> success(HttpStatus status) {
        return success(null, null, status);
    }

    /**
     * 成功返回
     *
     * @param object  返回对象
     * @param headers HttpHeaders对象
     * @param status  HttpStatus对象
     * @param <T>     T
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> success(T object, HttpHeaders headers, HttpStatus status) {
        if (headers == null) {
            headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
        }
        return new ResponseEntity<>(
                Result.<T>builder()
                        .data(object)
                        .code(ResultCode.SUCCESS.getCode())
                        .msg(ResultCode.SUCCESS.getMsg())
                        .trackId(MDC.get(Commons.TRACE_ID))
                        .build(), headers, status);
    }

    @SuppressWarnings("unchecked")
    protected <T> ResponseEntity directlyReturn(T object) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity(object, headers, HttpStatus.OK);
    }

    protected <T> ResponseEntity<Result<T>> fail(Result<T> result) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<>(result, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
